<cfinclude template="/ecada/header.cfm">
<cfif Not IsDefined("session.id")>
	<cfset session.message = "You must be logged in to view groups!">
	<cflocation url="/ecada/index.cfm">
</cfif>
<cfset UID = session.id>
<!--- get all the groupID's of the groups that the current user is part of --->

<cfquery name="GroupIDs" datasource="exp">

	SELECT *
	FROM groupusers
	WHERE UserID = #UID#

</cfquery>
<cfoutput>
		<div style="border-bottom: 1px solid  ##4297d7;">
			<h3>Create Group</h3>
			<div style="margin: 16px; padding-top: 16px">
				<p>
					On this page you can create, view and modify your group setiings. 
				</p>
			</div>
			<div style="margin: 16px; padding-top: 16px;">
				<form style="margin:0; " method="post" action="CreateGroup.cfm">
					<table>
						<tr>
							<td>
								Group Name:
							</td>
							<td>
								<input type="textbox" name="GroupName" id="GroupName" style="width: 400px;">
							</td>
						</tr>
						<tr>
							<td>
							</td>
							<td>
								<input type="Submit" value="Add Group" name="Submit">
							</td>
						</tr>
					</table>
				</form>
			</div>
		</div>
	</cfoutput>
<!---start outputting data--->
<cfoutput>
	<div id="ugcount" name="ugcount"><!---usergroup count--->
		<p>
			You are a member of the following group(s)
		</p>
	</div>
	<cfloop query="GroupIDs">
		<!---get GroupInformation and specific User info--->
		<cfquery name="GroupNames" datasource="exp">

			SELECT g.*, u.username "LeaderName", u.id "LeaderID"
			FROM groups g, user u
			WHERE g.id = #GroupID#
			AND ownerID = u.id

		</cfquery>
		
		<div style="margin: 0px;">

			<script type="text/javascript">

				function LeaveGroup()
				{
					//confirm box 
					return confirm("Are you definetly sure you want to leave this group?");
				}
				
				function DisbandGroup()
				{
					//First check
					return confirm("WARNING: you are the owner of this group. If you leave the group, the group will be disbanded and all experiments will become unaccessable! Do you want to continue?");
				}

				function RemoveUser()
				{
					return confirm("Are you sure you want to remove this user?");	
				}

				function PromoteUser()
				{
					return confirm("Are you sure you want promote this user, you will not be able to undo this action?");	
				}

			</script>
			<cfquery datasource="exp" name="gpUsers">

				SELECT u.username, u.id
				FROM user u, groupusers gp
				WHERE u.id = gp.UserID
				AND gp.GroupID = #GroupNames.id#
				ORDER BY u.username

			</cfquery>
			<table border=1>
				<tr>
					<td style="text-align: center;">Group name</td>
					<td style="text-align: center;" WIDTH="200px">#GroupNames.name#</td>
				</tr>
				<tr>
					<td style="text-align: center;">Group Leader</td>
					<td WIDTH="200px">
						<table><!--basic layout-->
							<tr>
								<td WIDTH="80%" style="text-align: left;">#GroupNames.LeaderName#</td>
								<td WIDTH="20%">
									<cfif session.id eq GroupNames.ownerID> <!---if the currently logged in user is the owner of the group--->
										<form method="post" action="DisbandGroup.cfm?g=#GroupNames.id#">
											<button class="ui-state-default ui-corner-all" title="Disbands the current group." type="Submit" value="Disband" onclick="return DisbandGroup();"><span style="margin: .3em;" class="ui-icon ui-icon-closethick"></span></button>
										</form>
									</cfif>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td style="text-align: center;">Group Members:</td>
					<td WIDTH="200px">
						<table>
							<cfloop query="gpUsers"><!---loop over users--->
								<cfif GroupNames.ownerID eq id>
									<cfcontinue><!---don't display owner under group memebers--->
								</cfif>
							
								<tr>
									<td WIDTH="80%" style="text-align: left;">#username#</td>
									<td WIDTH="10%">
									
										<cfif session.id eq GroupNames.ownerID><!---user equals owner--->
											<form method="post" action="RemoveFromGroup.cfm?u=#id#&g=#GroupNames.id#">
												<button class="ui-state-default ui-corner-all" title="Removes the selected user." type="Submit" value="Remove" onclick="return RemoveUser();"><span style="margin: .3em;" class="ui-icon ui-icon-closethick"></span></button>
											</form>
											</td>
											<td WIDTH="10%">
											<form method="post" action="PromoteToLeader.cfm?u=#id#&g=#GroupNames.id#">
												<button class="ui-state-default ui-corner-all" title="Promotes the selected user to group leader." type="Submit" value="Remove" onclick="return PromoteUser();"><span style="margin: .3em;" class="ui-icon ui-icon-arrowthick-1-n"></span></button>
											</form>
											
										<cfelse>
											<cfif session.id eq id>
												<form method="post" action="RemoveFromGroup.cfm?u=#id#&g=#GroupNames.id#">
													<button class="ui-state-default ui-corner-all" type="Submit" value="Leave" onclick="return LeaveGroup();"><span style="margin: .3em;" class="ui-icon ui-icon-closethick"></span></button>
												</form>
											</cfif>
										</cfif>
									</td>
								</tr>
							</cfloop>
							<cfif session.id eq GroupNames.ownerID>
								<cfquery datasource="exp" name="usersD">
										SELECT x.id, x.username
										FROM user x
										WHERE id NOT IN (
												SELECT u.id
												FROM user u, groupusers gp
												WHERE u.id = gp.UserID
												AND gp.GroupID = #GroupNames.id#
												)
										ORDER BY x.username, x.id
								</cfquery>
								<cfif usersD.recordcount neq 0>
									<tr>
										<form method="post" action="AddToGroup.cfm?g=#GroupID#">
											<td>
												<select name="Sel_users#GroupID#" id="Sel_users#GroupID#">
													<cfloop query="usersD">
														<option value="#usersD.id#">#usersD.username#</option>
													</cfloop>
												</select>
											</td>
											<td>
												<button class="ui-state-default ui-corner-all" title="Adds the selected user to the group." type="Submit" value="Add"><span style="margin: .3em;" class="ui-icon ui-icon-plusthick"></span></button>
											</td>
										</form>
									</tr>
								</cfif>
							</cfif>
						</table>
					</td>
				</tr>
			</table>
		</div>
	</cfloop>
</cfoutput>
<cfinclude template="/ecada/footer.cfm">

